<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>数组去重</h2>
<script>
    var a = ["1","2","2","3"];

    console.log(unique(a));
    console.log(unique2(a));
    console.log(unique3(a));
    console.log(unique4(a));

    function unique(arr){
        return Array.from(new Set(arr));
    }

    function unique2(arr){
        var newArr = [];
        for(var i in arr) {
            if(newArr.indexOf(arr[i]) == -1) {
                newArr.push(arr[i])
            }
        }
        return newArr;
    }

    function unique3(arr) {
        let hashTable = {};
        let newArr = [];
        for(let i=0,l=arr.length;i<l;i++) {
            if(!hashTable[arr[i]]) {
                hashTable[arr[i]] = true;
                newArr.push(arr[i]);
            }
        }
        return newArr;
    }

    function unique4(arr) {
        var newArr = [];
        var end; //end其实就是一道卡
        arr.sort();
        end = arr[0];
        newArr.push(arr[0]);
        for (var i = 1; i < arr.length; i++) {
            if (arr[i] != end) {
                newArr.push(arr[i]);
                end = arr[i]; //更新end
            }
        }
        return newArr;
    }


</script>
</body>
</html>